package com.huluobo.a2208parkdemo.visitor

import android.app.DatePickerDialog
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2208parkdemo.R
import com.huluobo.a2208parkdemo.databinding.ActivityAddVisitorBinding
import com.huluobo.base.mvvm.BaseActivity
import com.huluobo.base.net.Const
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import java.util.Calendar

@AndroidEntryPoint
class AddVisitorActivity : BaseActivity<ActivityAddVisitorBinding, VisitorViewModel>() {
    private var startTIme = 0
    private var endTime = 0
    override fun initData() {
        vm.addVisitorSuccess.observe(this) {
            ToastUtils.showLong("添加成功")
            finish()
        }
    }

    override fun initView() {
        binding.addVisitorHeader.headerTitle.text = "添加来访"
        binding.addVisitorHeader.headerBack.setOnClickListener { finish() }

        binding.addVisitorStartTimeIv.setOnClickListener {
            val calendar = Calendar.getInstance()
            DatePickerDialog(
                this,
                { view, year, month, dayOfMonth ->
                    binding.addVisitorStartTimeNowTv.text = "$year-${month + 1}-$dayOfMonth"
                    startTIme = dayOfMonth
                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)
            ).show()
        }

        binding.addVisitorEndTimeIv.setOnClickListener {
            val calendar = Calendar.getInstance()
            DatePickerDialog(
                this,
                { view, year, month, dayOfMonth ->
                    binding.addVisitorEndTimeNowTv.text = "$year-${month + 1}-$dayOfMonth"
                    endTime = dayOfMonth
                    val tampTime = endTime - startTIme
                    if (tampTime < 0) {
                        ToastUtils.showLong("来访结束时间不能小于开始时间")
                    } else {
                        binding.addVisitorTimeNowTv.text = "${tampTime}天"
                    }
                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)
            ).show()
        }

        binding.addVisitorCommit.setOnClickListener {
            val carNum = binding.addVisitorCarNumNowEt.text.toString()
            val desc = binding.addVisitorDescNowEt.text.toString()
            val startTime = binding.addVisitorStartTimeNowTv.text.toString()
            val endTime = binding.addVisitorEndTimeNowTv.text.toString()
            val time = binding.addVisitorTimeNowTv.text.toString()
            if (carNum.isNotEmpty() && desc.isNotEmpty() && startTime.isNotEmpty() && endTime.isNotEmpty() && time.isNotEmpty()) {
                val map = mutableMapOf<String, String>()
                map[Const.PARAM_VISITOR_CAR_NUM] = carNum
                map[Const.PARAM_VISITOR_DESC] = desc
                map[Const.PARAM_VISITOR_START] = startTime
                map[Const.PARAM_VISITOR_END] = endTime
                map[Const.PARAM_VISITOR_TIME] = time

                val json = Gson().toJson(map)
                val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                vm.addVisitor(body)

            } else {
                ToastUtils.showLong("请重新选择时间或者描述和车牌号不能为空")
            }
        }
    }
}